<template>
  <div class="cart spage">
     <myhead :back="true" title="我的购物车"></myhead>
      <van-notice-bar
          left-icon="volume-o"
          text="走过路过,千万不要错过,你吃不了亏,你上不了当,快快来嘛"
          :scrollable="true"
      />
      <div class="page-cart">
          <div class="cart-main" v-if="cartList.length>0">
             <van-swipe-cell class="my-wrap" v-for="(l,i) in cartList" :key="i">
                  <van-checkbox v-model="l.check" @change="changeCheck(l)"></van-checkbox>
                  <van-card
                    :num="l.count"
                    :price="l.good.price * l.count"
                    :desc="l.good.type.text +' / ¥ '+ l.good.price"
                    :title="l.good.name"
                    class="goods-card"
                    :thumb="l.good.img"
                >
                </van-card>
                  <van-stepper
                    class="step"
                    v-model="l.count"
                    theme="round"
                    button-size="22"
                    disable-input
                    @change="changeCount(l)"
                />
                <template #right>
                    <van-button @click="deleteOne(l,i)" class="delete-button" square text="删除" type="danger" />
                </template>
              </van-swipe-cell>
          </div>
           <van-empty @click="goto('good')" v-else  description="你的购物车还没有商品,请购买哟" />
      </div>
       <div class="bottom-bar" >
          <div class="select-info">
              <van-checkbox class="check-all" v-model="all" />
              <p>
              已选 <span>{{ amount }}</span> 件
              </p>
          </div>
          <div class="sum-info">
              <div class="desc">
              <div class="total-price">
                  合计: <span>¥ {{ total.toFixed(2) }}</span>
              </div>
              <p>应付总额不含运费</p>
              </div>
              <van-button type="info" class="btn" @click="gocomputed"
              >现在结算</van-button
              >
          </div>
        </div>
  </div>
</template>
<script>
export default {
  methods:{
    goto(){
      console.log("12321")
    },
    gocomputed(){
      this.$router.push({name:"pay",query:{amount:this.total.toFixed(2)}})
    },
    changeCheck(item){
        console.log(item)
        // 修改后台的是否选择
        // this.$ajax.updateCart(item.id,{
        //   check:item.check
        // })
        // .then(res=>{
        //   this.$toast("修改成功");
        // })
    },
    changeCount(l){
      console.log(l)
       this.$ajax.updateCart(l.id,{
          count:l.count
        })
        .then(res=>{
          this.$toast("数量修改成功");
          this.getCartList();
        })
    },
    deleteOne(l,i){
      // 删除
      this.$ajax.deleteCart(l.id)
      .then(res=>{
         var list = [...this.cartList]
         list.splice(i,1);
         this.changeCartList(list);
         this.$toast("删除成功");
      })
    }
  },
   data(){
    return {
      amount:0,
      total:0
    }
  },
  watch:{
    cartList:{
      deep:true,   // 深度监听
      handler(v){
        this.total = 0;
        this.amount = 0; 
        v.forEach(item=>{
          if(item.check){
              this.total += item.count  * item.good.price * item.good.discount / 10 
              this.amount += item.count
          }
        })
      }
    }
  },
  computed:{
    all:{
      get(){
        var flag = true;
        this.cartList.forEach(item=>{
          if(!item.check){
            flag = false;
          }
        })
        return flag; 
      },
      set(v){
        var list = this.cartList.map(item=>{
           item = {...item,check:v};
           return item;
        })
        this.changeCartList(list);
      }
    }
  },
  mounted(){
    this.cartList.forEach(item=>{
       if(item.check){
          this.total += item.count  * item.good.price * item.good.discount / 10 
          this.amount += item.count
       }
    })
  },
}
</script>





<style lang="scss" scoped>
.my-wrap {
  background-color: #fff;
}
::v-deep .van-swipe-cell__wrapper {
  display: flex;
  align-items: center;
}
::v-deep .van-card__title,
.van-card {
  width: 100%;
}
::v-deep .van-swipe-cell__right {
  display: flex;
  align-items: center;
}
::v-deep .van-nav-bar__title {
  color: rgba(0, 0, 0, 0.6);
  font-weight: 700;
  font-size: 18px;
}
::v-deep .van-nav-bar__content {
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}
::v-deep .van-checkbox {
  padding: 0 10px;
}

.shop-wrapper {
  background-color: #fff;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .ngcontent {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    img {
      width: 88px;
      height: 114px;
    }
    .mytitle {
      color: rgba(0, 0, 0, 0.15);
      margin: 25px 0 9px;
      font-size: 24px;
      font-weight: bold;
    }
    .tip {
      font-size: 16px;
      color: rgba(0, 0, 0, 0.15);
    }
    .jump {
      .btn {
        margin: 16px 1px 0;
        width: 142px;
        border-radius: 10px;
      }
      .buy {
        border-color: rgba(0, 0, 0, 0.12);
        color: rgba(0, 0, 0, 0.6);
        margin-left: 6px;
      }
    }
  }
}
.page-cart {
  padding-top: 1px;
  padding-bottom: 100px;
  .receive-coupon {
    display: flex;
    align-items: center;
    height: 62px;
    background: #ffe7e5;
    color: #d04f48;
    padding-left: 0.5rem;
    padding-right: 0.1rem;
    position: relative;
    img {
      width: 28px;
      height: 28px;
    }
    p {
      font-size: 16px;
      font-weight: 700;
      margin-left: 8px;
    }
    .arrow {
      position: absolute;
      right: 20px;
      top: 16px;
    }
  }
  .cart-main {
    padding: 20px 0;
    .checkbox {
      width: 18px;
      height: 18px;
      //   margin: 0 20px;
      //   padding: 0 20px;
    }
    .cart-item {
      flex: 1;
      display: flex;
      align-items: center;
      padding: 20px 0;
      width: 100%;
      img {
        width: 90px;
        height: 90px;
      }
      .item-info {
        flex: 1;
        margin-left: 13px;
        padding: 0 5px 0 0;
        .title {
          color: #000;
          font-weight: 700;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
          overflow: hidden;
          font-size: 14px;
        }
        .item-attrs {
          font-size: 14px;
          color: #7f7f7f;
          overflow: hidden;
          margin: 7px 0;
        }
        .item-price-container {
          color: #d44d44;
          font-size: 14px;
          font-weight: 700;
          span {
            color: #999;
            padding-left: 6px;
          }
        }
      }
    }
  }
}

.bottom-bar {
  display: flex;
  justify-content: space-between;
  background: #fafafa;
//   box-shadow: 0 -0.2rem 1rem rgb(0 0 0 / 10%);
  padding: 0 12px;
  width: 100%;
  height: 54px;
  position: fixed;
  bottom: 0px;
  .select-info {
    display: flex;
    align-items: center;
    .check-all {
      width: 24px;
      height: 24px;
      margin-top: 6px;
      margin-left: 8px;
      padding: 0;
    }
    p {
      color: #999;
      margin-left: 8px;
      span {
        font-weight: 900;
        font-size: 16px;
      }
    }
  }
  .sum-info {
    display: flex;
    align-items: center;
    .desc {
      font-weight: 700;
      color: #333;
      font-size: 12px;
      margin-right: 8px;
      span {
        color: #d44d44;
        font-size: 12px;
      }
      p {
        color: #666;
        margin-top: 8px;
      }
    }
  }
  .btn {
    width: 110px;
    height: 40px;
    border-radius: 10px;
  }
}

.my-wrap {
  position: relative !important;
  .step {
    position: absolute !important;
    top: 80px;
    left: 250px;
    z-index: 2000;
    ::v-deep .van-stepper__input{
      width:50px;
      height:50px;
    }
  }
}
</style>